//: Playground - noun: a place where people can play

import UIKit

let myConstant = 42
var myVariable : CGFloat = 70
myVariable=70
//显示转换
let lable = "the world is"
let width = 12
let widthLable = lable + String(width)

let apple = "zhu"
let orange = "tian wei"
let fullName = "my name is \(apple + orange)"

var emptyArray = [NSInteger]()
emptyArray=[123,2345]


var occupations = ["zhu":"123","tian":"234"]


let individualScores = [73,12,65,77]
var sumScrore = 0
for score in individualScores{
    sumScrore+=score
}
NSLog("%d", sumScrore)

var optionalString: String? = nil
print(optionalString == nil)
var optionalName: String? = "John Appleseed"
var greeting = "Hello!"
if  optionalString != nil {
    greeting = "Hello, \(String(describing: optionalName))"
}
let newGreet = "Hello" + (optionalName ?? "")


//默认值
let nickName: String? = "juvid"
let fullNames: String = "zhu tian wei"
let infoMalGreeting = "Hi \(nickName ?? fullNames)"

let interestingNumbers = [
    "Prime":[2,3,4,5,6],
    "Fibonacci":[32,54,65,76],
    "Square":[5,6,7,9]
]



var largest = 0
for (kind,numbers) in interestingNumbers{
    NSLog("%@ %@", kind,numbers)
}

//普通函数
func greet (name:String ,day:String) ->String{
    return "hello \(name),today is \(day)"
}
greet(name:"bod",day:"tuerday")

/// 多个返回值函数
func calculateStatistics (scores :[Int]) -> (min:Int ,max:Int, sum:Int){
    var mins = scores[0]
    var maxs = scores[1]
    var sums = 0
    for score in scores{
        if score > maxs {
            maxs=score;
        }else if score < mins{
            mins=score
        }
        sums+=score;
    }
    return (mins,maxs,sums)
}

let statistics = calculateStatistics(scores: [50,3,62,7,10])
NSLog("%d", statistics.sum)
NSLog("%d", statistics.2)

///可变参数
func sumOf(numbers: Int...) ->Int{
    var sum = 0
    for number in numbers{
        sum += number
    }
    return sum;

}
sumOf()
sumOf(numbers:42, 36, 12)

let base = 3
let power = 10
var answer = 1
for _ in 1...power {
    answer *= base
}
print("\(base) to the power of \(power) is \(answer)")



///嵌套函数
func returnFifteen() -> Int{
    var y = 10
    func add(){
        y += 5
    }
    add()
    return y
}
returnFifteen()
NSLog("%d", returnFifteen())


/// 函数作为返回值
func makeIncrementer(sum:Int) -> ((Int) -> Int) {
    func addOne(number: Int) -> Int {
        return 1 + number + sum
    }
    addOne(number: 15)
    return addOne
}
///increment变量是个函数addOne
var increment = makeIncrementer(sum: 10)
increment(7)

/// 函数当做参数传入另一个函数
func hasAnyMatcher (list:[Int] ,condition: (Int)/**传入函数的参数类型*/ -> Bool/**传入返回值类型*/) ->Bool{
    for item in list{
        if condition(item) {
            NSLog("对")
            return true
        }
    }
    NSLog("错")
    return false
}

func lessThanTen (number :Int) -> Bool{
    return number < 10
}
var numberss = [10,30,40,13,12]
hasAnyMatcher(list: numberss, condition: lessThanTen)

/**类和对象**/
class Shape{
    var numberOfside = 0
    func simpleDescription() -> String {
        return "A shape with \(numberOfside) sides"
    }
}
var shape = Shape();
shape.numberOfside=10;
shape.simpleDescription();

class NamedShape{
    var numberOfSides : Int = 0
    var name : String

    init(name:String) {
        self.name=name
    }
    func simpleDescription() -> String {
        return "A shape with \(numberOfSides) sides"
    }
}


class Square: NamedShape {
    var sideLength: Double
    init(sideLength: Double, name: String) {
        self.sideLength = sideLength
        super.init(name: name)
        numberOfSides = 4
    }
    func area() ->  Double {
        return sideLength * sideLength
    }
    override func simpleDescription() -> String {
        return "A square with sides of length \(sideLength)."
    }
}
let test = Square(sideLength: 5.2, name: "my test square")
test.area()
test.simpleDescription()

//**set和get方法**//
class EquilateralTriangle: NamedShape {
    var sideLength: Double = 0.0
    init(sideLength: Double, name: String) {
        self.sideLength = sideLength
        super.init(name: name)
        numberOfSides = 3
    }

    var perimeter: Double {
        get {
            return 3.0 * sideLength
        }
        set {
            sideLength = newValue / 3.0
        }
    }
    override func simpleDescription() -> String {
        return "An equilateral triagle with sides of length \(sideLength)."
    }
}
var triangle = EquilateralTriangle(sideLength: 3.1, name: "a triangle")
print(triangle.perimeter)
triangle.perimeter = 9.9
print(triangle.sideLength)

//**willset**//
class TriangleAndSquare {
    static func testMethod() -> TriangleAndSquare{
        return TriangleAndSquare.init(size: 12.5, name: "zhhh");
//        NSLog("类方法")
    }

    var triangle: EquilateralTriangle {
        willSet {
            square.sideLength = newValue.sideLength
        } }
    var square: Square {
        willSet {
            triangle.sideLength = newValue.sideLength
        }
    }
    init(size: Double, name: String) {
        square = Square(sideLength: size, name: name)
        triangle = EquilateralTriangle(sideLength: size, name: name)
    }
}
var triangleAndSquare = TriangleAndSquare.testMethod()
print(triangleAndSquare.square.sideLength)
print(triangleAndSquare.triangle.sideLength)
triangleAndSquare.square = Square(sideLength: 50, name: "larger square")
print(triangleAndSquare.triangle.sideLength)


enum Rank: Int {
    case Ace = 1
    case Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten
    case Jack, Queen, King
    func simpleDescription() -> String {
        switch self {
        case .Ace:
            return "ace"
        case .Jack:
            return "jack"
        case .Queen:
            return "queen"
        case .King:
            return "king"
        default:
            return String(self.rawValue)
        }
    } }
let ace = Rank.Queen
let aceRawValue = ace.rawValue

let funcValue = ace.simpleDescription()


protocol ExampleProtocol {
    var simpleDescription: String { get }
    mutating func adjust()
}


/**new**/
